<?php
/**
 * This is used as a helper for redirecting posts
 * Typically used by /ajax/ files
 */
class folio_redirect {

    // Public vars
    public $redirect_url = '';
    public $message = '';
    public $echo = '';
    public $object = null;

    /**
     * Return the html for the hidden input fields that will instruct the ajax
     * post page what to do as the result of a successful POST.
     *
     * @return string
     */
    public function html() {
        $name = array();
        $name[] = folio_redirect::htmlVariableName( 'redirect_url' );
        $name[] = folio_redirect::htmlVariableName( 'message' );
        $name[] = folio_redirect::htmlVariableName( 'echo' );

        $html = <<< END
    <input type="hidden" name="{$name[0]}" value="{$this->redirect_url}" />
    <input type="hidden" name="{$name[1]}" value="{$this->message}" />
    <input type="hidden" name="{$name[2]}" value="{$this->echo}" />
END;
        return $html;
    }

    /**
     * Called to return the html name of a class member.
     * Used because html() is not always usable when we're doing a lot of
     * ajax / js.  Instead, pull up the encoded name of one of the class vars.
     */
    public function htmlVariableName( $name ) {
        if( $name == 'redirect_url' ) {
            return 'folio_redirect_url';
        } elseif( $name == 'message' ) {
            return 'folio_redirect_message';
        } elseif( $name == 'echo' ) {
            return 'folio_redirect_echo';
        } else {
            trigger_error("Invalid $name passed to folio_redirect::htmlVariableName", E_USER_ERROR);
        }
    }


    /**
     * Grab the incoming folio_redirect variables from the passed POST varible and
     *     return a new folio_redirect object.
     * Note that these can be set thru js files, and not necessarily through
     *     the html() function.
     * @param _POST $p Post var, not needed, just a reminder that data is being used from it.
     */
    public function parse( $p = false ) {

        if( $p == false ) {
            $p = $_POST;
        }

        // Make sure we have post variables.
        if ( !( isset( $p['folio_redirect_url'] ) OR
             isset( $p['folio_redirect_message'] ) OR
             isset( $p['folio_redirect_echo'] ) )) {
            return false;
        }

        // Build vars
        $redirect = new folio_redirect;
        if ( isset($p['folio_redirect_url']) ) {
            $redirect->redirect_url = $p['folio_redirect_url'];
        }
        if ( isset($p['folio_redirect_message'] ) ) {
            $redirect->message = $p['folio_redirect_message'];
        }
        if( isset( $p['folio_redirect_echo'] ) ) {
            $redirect->echo = $p['folio_redirect_echo'];
        }

        // Make sure that some sort of response is required.
        if( strlen( $redirect->redirect_url . $redirect->message . $redirect->echo ) <= 0 ) {
            return false;
        }

        return $redirect;
    }

    /**
     * Called to actually do the actions that this class manages.
     *
     * @param $object = if not false, then used as the source object for any required field echo requests.
     */
    public function PerformRedirectActions( $object = false) {

        // Figure out how we should respond
        if( $this->redirect_url <> '' ) {
            header("Location: " . $this->redirect_url);
        }
        if( $this->message <> '' ) {
            $_SESSION['messages'] = array( $this->message );
        }
        if( $this->echo <> '' ) {
            $field = $this->echo;
            echo ''.$object->$field;
        }
    }

}

?>
